var displaymenu = false;

function incZIndex ( obj ) {
	obj.style.zIndex++;
}

function decZIndex ( obj ) {
	if ( obj.style.zIndex > obj.parentNode.style.zIndex + 1 )
		obj.style.zIndex--;
}

function hideMenu ( event ) {
	main_menu = document.getElementById('main_menu');
	if ( event && event.target ) debug("ize:" + event.target);
	if ( displaymenu
		&& !( event && event.target 
			&&  (event.target == main_menu
				|| (event.target.parentNode && event.target.parentNode == main_menu)
			)
		)
	) {
		if ( main_menu.hasChildNodes() )
		{
		    while ( main_menu.childNodes.length >= 1 )
		    {
		        main_menu.removeChild( main_menu.firstChild );       
		    } 
		}
		displaymenu = false;
		main_menu.style.display = "none";
	}
}

function showMenu ( obj ) {
	hideMenu( null );
	main_menu = document.getElementById('main_menu');
	main_menu.onmouseout = function ( event ) { hideMenu( event ); }
	
	div =  document.createElement('div');
	div.setAttribute('id','menu_deczind_div');
	div.setAttribute('class','menu_element');
	main_menu.appendChild(div);
	main_menu.appendChild(document.createElement('br'));

	deczind = document.createElement('a');
	deczind.id = "a1";
	deczind.href = "#";
	deczind.innerHTML = "send&nbsp;back";
	deczind.onclick = function () {
		decZIndex(obj);
	}
	div.appendChild(deczind);

	div =  document.createElement('div');
	div.setAttribute('id','menu_deczind_div');
	div.setAttribute('class','menu_element');
	main_menu.appendChild(div);
	main_menu.appendChild(document.createElement('br'));

	inczind = document.createElement('a');
	inczind.id = "a2";
	inczind.href = "#";
	inczind.innerHTML = "bring&nbsp;front";
	inczind.onclick = function () {
		incZIndex(obj);
	}
	div.appendChild(inczind);
	
	displaymenu = true;
	main_menu.style.display = "block";
}

function newMenu ( obj ) {
	var menu=document.createElement('div');
	menu.setAttribute('id', obj.id+'_menu');
	menu.setAttribute('class', 'menu_div');
	menu.onclick = function ( event ) {
		showMenu( obj );
	}
	return menu;
}

